<script setup>
import { ArrowRight, Picture } from "@element-plus/icons-vue";
import { getDetailApi } from "@/api/detail";
import { useRoute } from "vue-router";
import { ref, onMounted } from "vue";
import ImageView from "@/components/ImageView/index.vue";
import XtxSku from "@/components/XtxSku/index.vue";
import { useCartStore } from "@/stores/cart";
const route = useRoute();
const cartStore = useCartStore();
//商品详情存储
const categoryData = ref({});
const getDetail = async () => {
  const res = await getDetailApi(route.params.id);
  categoryData.value = res.result;
  console.log(categoryData.value);
};

onMounted(() => getDetail());
//Sku被操作时
let onSku = ref();
const skuChange = (specs) => {
  console.log(specs);
  onSku = specs;
};
//操作商品数量count
const count = ref(1);
const handleChange = (val) => {
  count.value = val;
};
//添加购物车
const addCart = () => {
  if (onSku.skuId) {
    cartStore.addCart({
      id: categoryData.value.id,
      name: categoryData.value.name,
      picture: categoryData.value.mainPictures[0],
      price: categoryData.value.price,
      count: count.value,
      skuId: onSku.skuId,
      arrtrsText: onSku.specsText, //商品规格文本
      selected: true, //商品是否选中
    });
  } else {
    ElMessage.warning("请选择商品规格");
  }
};
</script>
<template>
  <div class="detail container">
    <div class="bread" v-if="categoryData.categories">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: `/category/${categoryData.categories?.[1].id}` }">{{
          categoryData.categories?.[1].name
        }}</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: `/category/sub/${categoryData.categories?.[0].id}` }">{{
          categoryData.categories?.[0].name
        }}</el-breadcrumb-item>
        <el-breadcrumb-item>{{ categoryData.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 商品信息 -->
    <div class="info-container">
      <div>
        <div class="goods-info">
          <div class="media">
            <ImageView :imageData="categoryData.mainPictures"></ImageView>
            <ul class="goods-sales">
              <li>
                <p>销量人气</p>
                <p>{{ categoryData.salesCount }}</p>
                <p><i class="iconfont icon-task-filling"></i>销量人气</p>
              </li>
              <li>
                <p>商品评价</p>
                <p>{{ categoryData.commentCount }}</p>
                <p><i class="iconfont icon-comment-filling"></i>查看评价</p>
              </li>
              <li>
                <p>收藏人气</p>
                <p>{{ categoryData.collectCount }}</p>
                <p><i class="iconfont icon-favorite-filling"></i>收藏商品</p>
              </li>
              <li>
                <p>品牌信息</p>
                <p>100</p>
                <p><i class="iconfont icon-dynamic-filling"></i>品牌主页</p>
              </li>
            </ul>
          </div>
          <div class="spec">
            <!-- 商品信息区 -->
            <p class="g-name">{{ categoryData.name }}</p>
            <p class="g-desc">{{ categoryData.desc }}</p>
            <p class="g-price">
              <span><i>¥</i>{{ categoryData.price }}</span>
              <span><i>¥</i> {{ categoryData.oldPrice }}</span>
            </p>
            <div class="g-service">
              <dl>
                <dt>促销</dt>
                <dd>12月好物放送，App领券购买直降120元</dd>
              </dl>
              <dl>
                <dt>服务</dt>
                <dd>
                  <span>无忧退货</span>
                  <span>快速退款</span>
                  <span>免费包邮</span>
                  <a href="javascript:;">了解详情</a>
                </dd>
              </dl>
            </div>
            <!-- sku组件 -->
            <XtxSku :goods="categoryData" @change="skuChange"></XtxSku>
            <!-- 数据组件 -->
            <el-input-number v-model="count" :min="1" :max="10" @change="handleChange" />
            <!-- 按钮组件 -->
            <div>
              <el-button size="large" class="btn" @click="addCart"> 加入购物车 </el-button>
            </div>
          </div>
        </div>
        <div class="goods-footer">
          <div class="goods-article">
            <!-- 商品详情 -->
            <div class="goods-tabs">
              <nav>
                <a> 商品详情 </a>
              </nav>
              <div class="goods-detail">
                <!-- 属性 -->
                <!-- <ul class="attrs" v-if="detailsData.details.properties">
                  <li v-for="item in detailsData.details.properties" :key="item.value">
                    <span class="dt">{{ item.name }}</span>
                    <span class="dd">{{ item.value }}</span>
                  </li>
                </ul> -->
                <!-- 图片 -->
                <!-- <img v-for="img in categoryData.details.pictures" :src="img" :key="img" alt="" /> -->
              </div>
            </div>
          </div>
          <!-- 24热榜+专题推荐 -->
          <div class="goods-aside"></div>
        </div>
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss">
.goods-info {
  height: 600px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;

    .goods-sales {
      display: flex;
      justify-content: center;
      width: 400px;
      height: 140px;
      li {
        width: 100px;
        height: 79px;
        p {
          width: 100px;
          height: 20px;
          line-height: 20px;
          padding-top: 10px;
          font-size: 14px;
          text-align: center;
          align-items: center;
        }
      }
    }
  }
  .spec {
    width: 660px;
    height: 600px;
    padding: 30px 30px 30px 0;
    .g-name {
      font-size: 22px;
      font-weight: bold;
    }
    .g-desc {
      color: #999;
      margin-top: 10px;
    }
    .g-price {
      margin-top: 10px;
      span {
        color: #cf4444;
        margin-right: 10px;
        font-size: 22px;
        i {
          color: #cf4444 !important;
          font-size: 22px !important;
        }
      }
      :last-child {
        text-decoration: line-through;
        color: #999;
        font-size: 16px;
        i {
          color: #999 !important;
          font-size: 16px !important;
        }
      }
    }
    .g-service {
      background: #f5f5f5;
      width: 500px;
      padding: 20px 10px 0 10px;
      margin-top: 10px;
      dl {
        padding-bottom: 20px;
        display: flex;
        align-items: center;
        dt {
          width: 50px;
          color: #999;
        }
      }
    }
    .btn {
      margin-top: 20px;
      border-color: #27ba9b;
      background: #27ba9b;
      color: #fff;
    }
  }
}
.goods-footer {
  margin-top: 20px;
  display: flex;
  .goods-article {
    background-color: #fff;
    width: 940px;
    margin-right: 20px;
    .goods-tabs {
      nav {
        height: 70px;
        display: flex;
        padding: 0;
        font-size: 18px;
        position: relative;
        border-bottom: 1px solid #f5f5f5;
        a {
          width: 155px;
          height: 70px;
          line-height: 70px;
          padding: 0 40px;
          border-right: 1px solid #f5f5f5;
        }
      }
      .goods-detail {
        width: 940px;
        padding: 40px;
        .attrs {
          width: 860px;
          height: 89px;
          margin-bottom: 30px;
          display: flex;
          justify-content: space-between;
          flex-wrap: wrap;
          li {
            width: 430px;
            height: 20px;
            line-height: 20px;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            margin-bottom: 10px;
            width: 50%;
            .dt {
              width: 100px;
              height: 20px;
              line-height: 20px;
              color: #999;
            }
            .dd {
              width: 330px;
              height: 20px;
              line-height: 20px;
            }
          }
        }
      }
    }
  }
  .goods-aside {
    background-color: pink;
    width: 280px;
  }
}
</style>
